/*
 * CS_REQ_USER_HPSP_INFO.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */


#include "../../User.h"
#include "CS_REQ_USER_HPSP_INFO.h"

#include "../../NLogic/Space.h"
#include "../../NLogic/Monster.h"
#include "../../NLogic/Battle.h"

namespace ProjectZ { namespace NCommand { namespace NBattle {

void CS_REQ_USER_HPSP_INFO::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * CS_REQ_USER_HPSP_INFO	0x0518
	{
		u32	u32gid;
		s32	s32hp_variation;
		s32	s32sp_variation;
		u8	u8is_monster;
	}

	/// 던젼 결과 세부사항(던젼의 경우 파티원 모두에게 정보 내려줌)
	SN_REQ_USER_HPSP_INFO	0x0519
	{
		u32	u32gid;
		s32	s32current_hp;
		s32	s32current_sp;
		u8	u8is_monster;
	}
	 */

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_REQ_USER_HPSP_INFO NULL == pSpace");
		return;
	}

	PREPARE_PACKET;
//	Player_Info & player_info = pUser->player_info;

	int targethp = 0, targetsp = 0;

	int gid;
	int hp_gap;
	int sp_gap;
	int8_t is_monster;

	*pPacket >> gid >> hp_gap >> sp_gap >> is_monster;


	if (is_monster == 0)
	{
		if( NLogic::Space::SpaceType::BATTLEROYAL == pSpace->GetData()._spaceType )
		{
//			NLogic::Battle::Data & clsBattleRoyalData = pUser->GetBattleRoyalBattle()->GetData();

			NLogic::Battle * pBattle = pSpace->FindBattle(gid);
			if (NULL == pBattle)
			{
				DEBUGLOG("%s : BattleRoyal pBattle is NULL. gid=%d, user_seq=%d", __FUNCTION__, gid, pUser->GetUserSeq());
				return;
			}

			NLogic::Battle::Data & clsBattleRoyalData = pBattle->GetData();

			if (clsBattleRoyalData.HP <= 0 )
				return;

			clsBattleRoyalData.HP += hp_gap;
			if( clsBattleRoyalData.HP < 1 )
				clsBattleRoyalData.HP = 1;
			else if( clsBattleRoyalData.HP > clsBattleRoyalData.HP_MAX)
				clsBattleRoyalData.HP = clsBattleRoyalData.HP_MAX;

			clsBattleRoyalData.SP += sp_gap;
			if( clsBattleRoyalData.SP < 1 )
				clsBattleRoyalData.SP = 1;
			else if( clsBattleRoyalData.SP > clsBattleRoyalData.SP_MAX)
				clsBattleRoyalData.SP = clsBattleRoyalData.SP_MAX;

			targethp = clsBattleRoyalData.HP;
			targetsp = clsBattleRoyalData.SP;
		}
		else
		{
			NLogic::Battle::Data & clsBattleData = pUser->GetBattle()->GetData();

			if (clsBattleData.HP <= 0 )
				return;

			// user
			clsBattleData.HP += hp_gap;
			if( clsBattleData.HP < 1 )
				clsBattleData.HP = 1;
			else if( clsBattleData.HP > clsBattleData.HP_MAX)
				clsBattleData.HP = clsBattleData.HP_MAX;

			clsBattleData.SP += sp_gap;
			if( clsBattleData.SP < 1 )
				clsBattleData.SP = 1;
			else if( clsBattleData.SP > clsBattleData.SP_MAX)
				clsBattleData.SP = clsBattleData.SP_MAX;

			targethp = clsBattleData.HP;
			targetsp = clsBattleData.SP;
		}
	}
	else
	{
		NLogic::Monster * pMonster = pSpace->GetMonster(gid);
		if (NULL != pMonster)
		{
			targethp = pMonster->ChangeHP(hp_gap);
			targetsp = pMonster->ChangeSP(sp_gap);

			if (targethp <= 0)
				return;
		}
	}

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pNoti << gid << targethp << targetsp << is_monster;
	pNoti->MakePacket(CMD_SC_REQ_USER_HPSP_INFO);

	pSpace->SendAll(pNoti);
}

} /*NBattle*/ } /*NCommand*/ } /*ProjectZ*/
